package com.kgw.gatewaybluetooth.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.kgw.gatewaybluetooth.bean.StudentInfo;

import java.util.List;

/**
 * Created by jaydenma on 16/6/28.
 */
public class StudentDao {

    private static StudentDao dao;
    private DBHelper dbHelper;


    private StudentDao(Context context) {
        dbHelper = DBHelper.getInstance(context);
    }

    public static StudentDao getInstance(Context context) {
        dao = new StudentDao(context);
        return dao;
    }

    public boolean add(StudentInfo bean) {
        try {
            final ContentValues values = dbHelper.getContentValues(DBHelper.TYPE_INSERT, bean);
            dbHelper.getSqliteDatabaseW().insert(StudentTable.table, null, values);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            dbHelper.closeSqliteDatabaseW();
        }
        return false;
    }


    public int deleteAll() {
        int count = dbHelper.getSqliteDatabaseW().delete(StudentTable.table, null, null);
        dbHelper.closeSqliteDatabaseW();
        return count;
    }

    public boolean update(StudentInfo bean) {
        final String selection = StudentTable.StudentColums.ID + " = ?";
        final String[] args = new String[]{
                bean.getId()
        };
        int v = 0;
        try {
            ContentValues values = dbHelper.getContentValues(DBHelper.TYPE_UPDATE, bean);
            v = dbHelper.getSqliteDatabaseW().update(StudentTable.table, values, selection, args);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            dbHelper.closeSqliteDatabaseW();
        }
        return v > 0 ? true : false;
    }


    public boolean addList(List<StudentInfo> list) {
        SQLiteDatabase db = null;
        try {
            db = dbHelper.getSqliteDatabaseW();
            for (int i = 0; i < list.size(); i++) {
                final ContentValues values = dbHelper.getContentValues(DBHelper.TYPE_INSERT, list.get(i));
                db.insert(StudentTable.table, null, values);
            }
            db.setTransactionSuccessful();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            try {
                db.endTransaction();
                db.close();
                dbHelper.closeSqliteDatabaseW();
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }
        return true;
    }

    public StudentInfo getByCardNo(String cardNo) {
        StudentInfo student = null;
        final String selection = StudentTable.StudentColums.CARD_NO + "=?";
        final String[] args = new String[]{cardNo};
        Cursor c = dbHelper.getSqliteDatabaseR().query(StudentTable.table, null, selection, args, null, null, null);
        try {
            if (c != null && c.getCount() > 0) {
                c.moveToFirst();
                student = dbHelper.getObject(StudentInfo.class, c);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            dbHelper.closeCursor(c);
            dbHelper.closeSqliteDatabaseR();
        }
        return student;
    }

    public StudentInfo getById(String Id) {
        StudentInfo student = null;
        final String selection = StudentTable.StudentColums.ID + "=?";
        final String[] args = new String[]{Id};
        Cursor c = dbHelper.getSqliteDatabaseR().query(StudentTable.table, null, selection, args, null, null, null);
        try {
            if (c != null && c.getCount() > 0) {
                c.moveToFirst();
                student = dbHelper.getObject(StudentInfo.class, c);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            dbHelper.closeCursor(c);
            dbHelper.closeSqliteDatabaseR();
        }
        return student;
    }

    public StudentInfo getByTeacherId(String Id) {
        StudentInfo student = null;
        final String selection = StudentTable.StudentColums.TEACHERID + "=?";
        final String[] args = new String[]{Id};
        Cursor c = dbHelper.getSqliteDatabaseR().query(StudentTable.table, null, selection, args, null, null, null);
        try {
            if (c != null && c.getCount() > 0) {
                c.moveToFirst();
                student = dbHelper.getObject(StudentInfo.class, c);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            dbHelper.closeCursor(c);
            dbHelper.closeSqliteDatabaseR();
        }
        return student;
    }

}
